<template>
  <view class="table-body">
    <view class="parent-row" @click="toggleExpand()">
      <view class="col name">
        <uni-icons :type="!item.goodsTitle && expanded!=null ? (expanded ? 'arrowup' : 'arrowdown'):''" size="16"
                   color="#666"/>
        {{ item.goodsTitle || item.shopName }}
      </view>
      <view class="col">{{item.inventoryQuantity || item.quantity }}</view>
      <view class="col">{{ item.totalInventoryQuantity || item.samePeriod }}</view>
      <view class="col">{{ item.totalpercentage }}
        <view class="icon-group" v-if="this.item.totalpercentage">
          <image :src="icon" class="icon"/>
        </view>
      </view>
    </view>

    <view v-if="expanded" class="child-rows">
      <view v-for="child in childList" :key="child.id" class="child-row">
        <view class="child-row-click">
          <view class="col name">{{ child.staff_name }}</view>
          <view class="col">{{ child.quantity }}</view>
          <view class="col">{{ child.samePeriod }}</view>
          <view class="col">{{ child.totalpercentage }}
            <view class="icon-group" v-if="child.totalpercentage">
              <image :src="iconC(child.totalpercentage)" class="icon"/>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>
<script>
import UniLoadMore from "../../../uni_modules/uni-load-more/components/uni-load-more/uni-load-more.vue";
import SMS from "../../functional/popup/SMS.vue";
import UniIcons from "../../../uni_modules/uni-icons/components/uni-icons/uni-icons.vue";
import _ from "lodash";
import {getAction} from "../../../utils/action";

export default {
  name: "saleListView",
  components: {UniLoadMore, SMS, UniIcons},
  props: {
    item: Object,
    ShopName: String,
    classifyName: String,
    classifyRange: Array,
    timeRange: Array, // 存储时间段[开始日期,结束日期]
  },
  computed: {
    icon() {
      if (!this.item.totalpercentage) return '../static/xintu/down.png'
      return Number(this.item.totalpercentage.replace("%", "")) > 0 ? '../static/xintu/top.png' : '../static/xintu/down.png'
    }
  },
  data() {
    return {
      childList: [], // 第二级
      dowList: [], //最底级
      expanded: false,
      childExpanded: false,
      loadingStatus: 'more',
      url: {
        shopInventoryList: "api/boss/saleStaffList"
      }
    }
  },
  mounted() {
    this.expanded = this.item.expanded
  },
  methods: {
    // 切换展开状态
    async toggleExpand() {
      if (item.goodsTitle == '合计') return
      this.$modal.loading("加载中！请稍后...")
      this.$set(this, 'expanded', !this.expanded); // 只需这一行
      let queryParam = {}
      if (this.timeRange.length == 2) {
        const [startDate, endDate] = this.timeRange;
        queryParam.startTime = startDate
        queryParam.endTime = endDate
      }
      console.log(this.classifyRange)
      if (this.classifyRange.length > 0) {
        queryParam.classifyId = this.classifyRange[0].value
      }
      queryParam.classifyName = this.classifyName
      queryParam.id = this.item.shopId
      let res = await getAction(this.url.shopInventoryList, queryParam)
      if (res.success) {
        this.childList = _.cloneDeep(res.result)
      }
      this.$nextTick(() => {
        this.$modal.closeLoading()
        this.loadingStatus = 'noMore';
      })
    },
    iconC(totalpercentage) {
      if (!totalpercentage) return '../static/xintu/down.png'
      return Number(totalpercentage.replace("%", "")) > 0 ? '../static/xintu/top.png' : '../static/xintu/down.png'
    }
  }
}
</script>
<style scoped lang="scss">
.parent-row {
  background-color: #fff;
  display: grid;
  grid-template-columns: 2fr 1fr 1fr 0.8fr;
  align-items: center;
  padding: 25rpx 0;
  border-bottom: 1rpx solid #eee;
}

.child-row {
  border-bottom: 1rpx solid #eee;

  &::before {
    content: "";
    position: absolute;
    left: 20rpx;
    top: 50%;
    width: 15rpx;
    height: 1px;
  }

  .child-row-click {
    height: 75rpx;
    grid-template-columns: 2fr 1fr 1fr 0.8fr;
    display: grid;
    align-items: center;
    position: relative;
  }
}

.grandchild-row {
  padding-left: 80rpx; // 三级缩进
  border-top: 1rpx solid #eee;

  .grandchild-row-click {
    height: 75rpx;
    grid-template-columns: 2fr 1fr 1fr 0.8fr;
    display: grid;
    align-items: center;
    position: relative;
  }
}

.col {
  flex: 1;
  text-align: center;
  font-size: 22rpx;
  color: #333;

  .icon-group {
    display: inline-flex;
    align-items: center;
    vertical-align: middle;

    // 添加过渡动画
    transition: transform 0.3s ease;

    &:active {
      transform: scale(0.9); // 点击反馈
    }

    .icon {
      width: 1em; // 使用相对单位
      height: 1em;
      vertical-align: -0.15em;
      fill: currentColor; // 继承父级颜色
    }
  }
}

.name {
  text-align: left !important;
  padding-left: 55rpx;
}

.child-row .name {
  padding-left: 80rpx;
}

.action {
  display: flex;
  justify-content: center;
}

.child-row {
  border-bottom: 1rpx solid #eee;

  &::before {
    content: "";
    position: absolute;
    left: 20rpx;
    top: 50%;
    width: 15rpx;
    height: 1px;
  }

  .child-row-click {
    height: 75rpx;
    grid-template-columns: 2fr 1fr 1fr 0.8fr;
    display: grid;
    align-items: center;
    position: relative;
  }
}
</style>